package com.clkj.xiuzheng.dao.shangcheng;

import com.clkj.xiuzheng.pojo.shangcheng.PromoterApply;
import com.clkj.xiuzheng.pojo.shangcheng.SUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

import java.util.List;

@Repository
public interface ScUserMapper  extends Mapper<SUser>, MySqlMapper<SUser> {
    @Select({
            "<script>",
            "SELECT u1.*,u2.user_name as promoterName,pc.company_name as companyName FROM t_xz_user u1 left join t_xz_user u2 on u1.parent_id =u2.user_id  left join t_xz_productCompany pc on u1.company_id = pc.company_id ",
            "where u1.user_type!=0 ",
            "<if test = 'companyId!=null'>",
            "and u1.company_id = #{companyId} ",
            "</if>",
            "<if test = 'userName!=null'>",
            "and u1.user_name like '%${userName}%' ",
            "</if>",
            "<if test = 'startTime!=null'>",
            "and u1.create_time &gt;= #{startTime} ",
            "</if>",
            "<if test = 'endTime!=null'>",
            "and u1.create_time &lt;= #{endTime} ",
            "</if>",
//            "GROUP BY u1.company_id,u1.create_time,u1.diamond_num,u1.invite_code,u1.open_id,u1.user_balance,u1.user_id,u1.user_img,u1.user_name,u1.user_password,u1.user_phone,u1.user_score,u1.user_type,u1.vip_level,u2.user_name,u1.parent_id,pc.company_name,u1.used_points,u1.share_id"
            "</script>"
    })
    List<SUser> getUserList(Integer companyId, String userName, String startTime, String endTime);


    @Select({
            "<script>",
            "SELECT * FROM t_xz_user WHERE user_type=2",
            "<if test = 'companyId!=null'>",
            "and company_id = #{companyId} ",
            "</if>",
            "<if test = 'userName!=null'>",
            "and user_name like '%${userName}%' ",
            "</if>",
            "<if test = 'startTime!=null'>",
            "and create_time &gt;= #{startTime} ",
            "</if>",
            "<if test = 'endTime!=null'>",
            "and create_time &lt;= #{endTime} ",
            "</if>",
            "</script>"
    })
    List<SUser> getManagerList(@Param("companyId") Integer companyId, @Param("userName") String userName, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Insert("insert into t_xz_user values (#{userName},#{userPhone},#{userPassword},#{userScore},#{inviteCode},#{userBalance},#{diamondNum},#{openId}," +
            "#{companyId},#{userType},#{vipLevel},#{createTime},#{userImg},#{parentId},#{usedPoints},#{shareId},#{hasZg},#{yqm})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    void addUser(SUser user);

    @Select(
            "<script>"+
            "SELECT a.user_id,a.user_name,a.user_phone,c.company_name,c.company_id,a.user_type,isnull(b.total_money,0) as total_money ," +
                    "isNull(b.grant_money,0) as grant_money ,isNull(b.recharge_num,0) as recharge_num ,isNull(b.recharge_money,0) as recharge_money " +
                    " FROM t_xz_user a left join t_xz_userCommission   b on a.user_id = b.user_id " +
                    "left join t_xz_productCompany c on a.company_id = c.company_id " +
                    "  WHERE a.user_type=2"+
            "<if test = 'userName!=null'> and a.user_name like '%${userName}%'  </if>"+
            "<if test = 'companyId!=1'> and a.company_id  = #{companyId} </if>"+
            "</script>"
    )
    List<SUser> getpromoterList(String userName,Integer companyId);

    @Select("select a.*,b.user_name as shareName,c.user_name as promoterName from t_xz_user a  left join t_xz_user b on a.share_id = b.user_id " +
            "left join t_xz_user c on a.parent_id = c.user_id where a.user_id = #{userId} ")
    SUser listParentOrShare(@Param("userId")Integer userId);

    @Select("select * from t_xz_user where share_id = #{userId}")
    List<SUser> listMyUsers(@Param("userId")Integer userId);

    @Update(" update t_xz_user set user_balance -= #{money} where user_id = #{userId}")
    void updateMoney(Double money,Integer userId);

    @Update("update t_xz_user set user_type = 1 ,yqm = null where user_id = #{userId}")
    void updateUserTypeDown(Integer userId);

    @Update("update t_xz_user set parent_id = 0,yqm=null where user_id = #{userId}")
    void updateUserParent(Integer userId);

    @Select({
            "<script>",
            "SELECT u1.*,u2.user_name as promoterName,pc.company_name as companyName FROM t_xz_user u1 left join t_xz_user u2 on u1.parent_id =u2.user_id  left join t_xz_productCompany pc on u1.company_id = pc.company_id ",
            "where u1.user_type!=0 ",
            "<if test = 'userName!=null'>",
            "and u1.user_name like '%${userName}%' ",
            "</if>",
            "<if test = 'startTime!=null'>",
            "and u1.create_time &gt;= #{startTime} ",
            "</if>",
            "<if test = 'endTime!=null'>",
            "and u1.create_time &lt;= #{endTime} ",
            "</if>",
//            "GROUP BY u1.company_id,u1.create_time,u1.diamond_num,u1.invite_code,u1.open_id,u1.user_balance,u1.user_id,u1.user_img,u1.user_name,u1.user_password,u1.user_phone,u1.user_score,u1.user_type,u1.vip_level,u2.user_name,u1.parent_id,pc.company_name,u1.used_points,u1.share_id"
            "</script>"
    })
    List<SUser> getAllUserList(@Param("userName") String userName, @Param("startTime") String startTime, @Param("endTime") String endTime);
    @Select("select count(*) from t_xz_user where company_id = #{id}")
    Integer getUserNum(Integer id);
    @Select("select count(*) from t_xz_user where company_id = #{id} and user_type=#{userType} and create_time between #{startTime} and #{endTime}")
    Integer getUserNumByMonth(@Param("id") Integer id, @Param("startTime") String startTime, @Param("endTime") String endTime,@Param("userType") Integer userType);
    @Select("select count(*) from t_xz_user where user_type=#{userType} and create_time between #{startTime} and #{endTime} ")
    Integer getAlluserCount();
}
